package com.ibeetl.admin.console.service;

import com.ibeetl.admin.console.dao.DictConsoleDao;
import com.ibeetl.admin.console.web.dto.DictExcelImportData;
import com.ibeetl.admin.core.entity.CoreDict;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.service.BaseService;
import com.ibeetl.admin.core.util.ExcelError;
import com.ibeetl.admin.core.util.PlatformException;
import org.apache.poi.ss.util.CellReference;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * CoreDict Service
 */

@Service
@Transactional
public class DictConsoleService extends BaseService<CoreDict>{

    @Autowired
    private DictConsoleDao dictDao;

    public PageQuery<CoreDict> queryByCondition(PageQuery query){
        PageQuery ret =  dictDao.queryByCondition(query);
        queryListAfter(ret.getList());
        return ret;
    }

    public void batchDelCoreDict(List<Long> ids){
        try {
            //TODO,找到数据字典所有子类，设置删除标记
            dictDao.batchDelCoreDictByIds(ids);
        } catch (Exception e) {
            throw new PlatformException("批量删除CoreDict失败", e);
        }
    }
    public List<CoreDict> queryExcel(PageQuery<CoreUser> query) {
        //同查询，不需要额外数据
        PageQuery ret =  dictDao.queryByCondition(query);
        queryListAfter(ret.getList());
        return ret.getList();

    }
    /**
     *  参考：dict_mapping.xml
     * @param list
     * @return
     */
    public void batchInsert(List<DictExcelImportData> list) {
        int dataStartRow = 2;
        final Map<Integer,DictExcelImportData> map = new HashMap<>();
        list.forEach((item)->map.put(item.getExcelId(), item));
        //逐个按照顺序导入
        for(DictExcelImportData item:list) {
            CoreDict dict = new  CoreDict();
            dict.setName(item.getName());
            dict.setRemark(item.getRemark());
            dict.setType(item.getType());
            dict.setTypeName(item.getTypeName());
            dict.setValue(item.getValue());

            //设置父字典
            if(item.getParentExcelId()!=0) {
                DictExcelImportData parentItem =  map.get(item.getParentExcelId());
                if(parentItem==null) {
                    //硬编码，TODO,用reader缺少校验，替换手写导入
                    int row = item.getExcelId()+dataStartRow;
                    throwImporError(row,5,"未找到父字典");
                }
                if(parentItem.getId()==null) {
                    int row = item.getExcelId()+dataStartRow;
                    throwImporError(row,5,"父字典未被导入，请先导入父字典");
                }
                dict.setParent(parentItem.getId());
            }
            dict.setCreateTime(new Date());
            //导入前先查找是否有此值
            CoreDict template = new CoreDict();
            template.setType(dict.getType());
            template.setValue(dict.getValue());
            CoreDict dbDict = dictDao.templateOne(template);
            if(dbDict!=null) {
                int row = item.getExcelId()+dataStartRow;
                throwImporError(row,0,"字典数据已经存在");
            }
            dictDao.insert(dict);

            item.setId(dict.getId());
            dataStartRow++;
        }


    }

    private void throwImporError(int row,int col,String msg) {
        ExcelError error = new ExcelError();
        CellReference cr = new CellReference(row,col,false,false);
        error.setCell(cr.formatAsString());
        error.setMsg(msg);
        throw new PlatformException("导入错误在:"+error.getCell()+","+msg);
    }

    /**
     ** 功能描述:获取系统配置的申报年度参数 获取不到取当年度
     ** @author liujun
     ** @date 2020/1/8 9:55
     **/
    public String getDeclareYear(){
        PageQuery page = new PageQuery();
        page.setPara("type", "systemDeclareYear");

        PageQuery<CoreDict> pageQuery = this.queryByCondition(page);
        List<CoreDict> dicts = pageQuery.getList();
        /**
         *
         */
        if(dicts != null && dicts.size() > 0){
            return dicts.get(0).getValue();
        }else{
            Integer year = Calendar.getInstance().get(Calendar.YEAR);
            String yearStr = year.toString();
            return yearStr;
        }
    }

    /**
     ** 功能描述:获取系统配置的人才卡生效开始日期  无配置则取当前日期
     ** @author liujun
     ** @date 2020/1/8 9:51
     **/
    public String getTelentStartDate(){
        PageQuery page = new PageQuery();
        page.setPara("type", "telentStartDate");

        PageQuery<CoreDict> pageQuery = this.queryByCondition(page);
        List<CoreDict> dicts = pageQuery.getList();
        /**
         *
         */
        if(dicts != null && dicts.size() > 0){
            return dicts.get(0).getValue();
        }else{
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            return formatter.format(date);
        }
    }
}